package org.jeecg.modules.cw.service.impl;

import org.jeecg.modules.cw.entity.CwZfcgbTi;
import org.jeecg.modules.cw.entity.CwZfcgb;
import org.jeecg.modules.cw.mapper.CwZfcgbMapper;
import org.jeecg.modules.cw.mapper.CwZfcgbTiMapper;
import org.jeecg.modules.cw.service.ICwZfcgbTiService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 政府采购提交表
 * @Author: jeecg-boot
 * @Date:   2025-09-11
 * @Version: V1.0
 */
@Service
public class CwZfcgbTiServiceImpl extends ServiceImpl<CwZfcgbTiMapper, CwZfcgbTi> implements ICwZfcgbTiService {

	@Autowired
	private CwZfcgbTiMapper cwZfcgbTiMapper;
	@Autowired
	private CwZfcgbMapper cwZfcgbMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(CwZfcgbTi cwZfcgbTi, List<CwZfcgb> cwZfcgbList) {
		cwZfcgbTiMapper.insert(cwZfcgbTi);
		if(cwZfcgbList!=null && cwZfcgbList.size()>0) {
			for(CwZfcgb entity:cwZfcgbList) {
				//外键设置
				entity.setMainId(cwZfcgbTi.getId());
				cwZfcgbMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(CwZfcgbTi cwZfcgbTi,List<CwZfcgb> cwZfcgbList) {
		cwZfcgbTiMapper.updateById(cwZfcgbTi);
		
		//1.先删除子表数据
		cwZfcgbMapper.deleteByMainId(cwZfcgbTi.getId());
		
		//2.子表数据重新插入
		if(cwZfcgbList!=null && cwZfcgbList.size()>0) {
			for(CwZfcgb entity:cwZfcgbList) {
				//外键设置
				entity.setMainId(cwZfcgbTi.getId());
				cwZfcgbMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		cwZfcgbMapper.deleteByMainId(id);
		cwZfcgbTiMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			cwZfcgbMapper.deleteByMainId(id.toString());
			cwZfcgbTiMapper.deleteById(id);
		}
	}
	
}
